//
//  UserInfoViewController.m
//  CarLifes
//
//  Created by yu on 2017/6/30.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "UserInfoViewController.h"
#import "UserInfoHeader.h"
#import "UserInfoCell.h"
#import "EquipmentModel.h"

#define kReuseIdentifier @"UserInfoCellId"

@interface UserInfoViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArr;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (@available(iOS 13.0, *)) {
        UIColor *bgColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kBackgroundColor;
            } else {
                return UIColor.systemBackgroundColor;
            }
        }];
        self.view.backgroundColor = bgColor;
    } else {
        self.view.backgroundColor = kBackgroundColor;
    }
    
    self.navigationItem.title = @"用户信息";
    //self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.tableView registerNib:NIB_NAMED(@"UserInfoCell") forCellReuseIdentifier:kReuseIdentifier];
    
    [self loadUserInfoData];
}

- (void)loadUserInfoData
{
    [self.requestTool loadEquipmentListByPage:1 size:1 status:@"" actStatus:@"" str:self.eqNo isNeedDeptId:self.isNeedDeptId show:YES success:^(NSArray *equipments,NSArray *equipCounts) {
        self.dataSource = [NSMutableArray arrayWithArray:equipments];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        PPLog(@"load eq list error = %@",error);
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSInteger integer = 0;
    switch (section) {
        case 0: integer = 5; break;
        case 1: integer = 4; break;
        case 2: integer = 4; break;
    }
    return integer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseIdentifier];
    EquipmentModel *model = self.dataSource.firstObject;
    
    if (indexPath.section == 0)
    {
        switch (indexPath.row) {
            case 0: cell.nameLabel.text = NSStringFormat(@"设备号：%@",model.eqNo?:@"");break; // 设备号
            case 1: cell.nameLabel.text = NSStringFormat(@"型  号：%@",model.eqModel?:@"");break; // 型号
            case 2: cell.nameLabel.text = NSStringFormat(@"SIM 卡号：%@",model.sim?:@""); break; // SIM卡号
            case 3: cell.nameLabel.text = NSStringFormat(@"出厂时间：%@",model.createTime?:@""); break; // 出厂时间
            case 4: cell.nameLabel.text = NSStringFormat(@"到期时间：%@",model.expTime?:@""); break; // 到期时间
            default:break;
        }
    }
    else if (indexPath.section == 1)
    {
        switch (indexPath.row) {
            case 0: cell.nameLabel.text = NSStringFormat(@"车主姓名：%@",model.ownerName?:@""); break;
            case 1: cell.nameLabel.text = NSStringFormat(@"车主电话：%@",model.phone?:@""); break; // 车主电话
            case 2: cell.nameLabel.text = NSStringFormat(@"身份证号：%@",model.owenerId?:@""); break; // 身份证号
            case 3: cell.nameLabel.text = NSStringFormat(@"合同号：%@",model.contractNo?:@""); break; // 合同号
            default:break;
        }
    }
    else
    {
        switch (indexPath.row) {
            case 0: cell.nameLabel.text = NSStringFormat(@"车牌号：%@",model.plateNum?:@""); break; // 车牌号
            case 1: cell.nameLabel.text = NSStringFormat(@"车辆型号：%@",model.vModel?:@""); break; // 车辆型号
            case 2: cell.nameLabel.text = NSStringFormat(@"发动机号：%@",model.engineNo?:@""); break; // 发动机号
            case 3: cell.nameLabel.text = NSStringFormat(@"车架号：%@",model.vin?:@""); break; // 车架号
            default:break;
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

#pragma mark - UIPasteboard -
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return action == @selector(copy:) ? YES : NO;
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    
    if (action == @selector(copy:)) {
        
        UserInfoCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        
        NSString *copyStr = [NSString stringWithFormat:@"%@",cell.nameLabel.text];
        [pasteBoard setString:copyStr];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UserInfoHeader *headerView = [[UserInfoHeader alloc] init];
    switch (section) {
        case 0:
            headerView.titleLabel.text = @"设备详情";
            break;
        case 1:
            headerView.titleLabel.text = @"车主详情";
            break;
        case 2:
            headerView.titleLabel.text = @"车辆详情";
            break;  
        default:
            break;
    }
    return headerView;
}

- (NSArray *)dataArr {
    if (_dataArr == nil) {
        _dataArr = @[
  @[@{@"name":@"设备号(Id):",@"title":@"1234567890"},
    @{@"name":@"型    号:",@"title":@"GT02A"},
    @{@"name":@"SIM 卡号:",@"title":@"9876543210"},
    @{@"name":@"出厂时间:",@"title":@"2017-11-11"},
    @{@"name":@"到期时间:",@"title":@"2017-12-12"},
    @{@"name":@"用户到期:",@"title":@"2018-12-12"}
   ],
  
  @[@{@"name":@"车主姓名:",@"title":@"何孝天"},
    @{@"name":@"车主电话:",@"title":@"15866786713"},
    @{@"name":@"身份证号:",@"title":@"370126199909097135"},
    @{@"name":@"合同号:",@"title":@"123456789"}
   ],
  
  @[@{@"name":@"车牌号:",@"title":@"鲁A8888"},
    @{@"name":@"车辆型号:",@"title":@"小型车辆"},
    @{@"name":@"发动机号:",@"title":@"345543345543"},
    @{@"name":@"车架号:",@"title":@"LKV2A21K3C4088713"}
    ]
                     ];
    }
    return _dataArr;
}

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
